package com.cpr.demo.thread.itl2;

/**
 * @author Chen Panrui
 * created at 2021/9/8 15:14
 */
public class ContextHolder {
    private static final InheritableThreadLocal<Context> contextStore = new InheritableThreadLocal<Context>();

    /**
     * 获取上下文
     *
     * @return
     */
    public static Context getContext() {
        Context context = contextStore.get();
        // 没有保存上下文,则返回对象
        if (context == null) {
            return new Context();
        }
        return context;
    }

    /**
     * 设置上下文
     *
     * @param context
     */
    public static void setContext(Context context) {
        contextStore.set(context);
    }

    /**
     * 清空
     */
    public static void clear() {
        contextStore.remove();
    }
}
